/*
	Released under this MIT License:
		
	Copyright (c) 2010 Bart van der Schoor
		
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
		
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
		
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
	
 */
package bruce
{
	import flash.display.MovieClip;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	/**
	 *
	 *	Blocker
	 *	
	 *	 draw a full-screen-resizing-button, like an invisible mcHit, but better, and stage.align proof 
	 *	 
	 *	 optionally specifiy on top, with color or alpha-transparent, like a (colored) clickable background?
	 *	
	 *	 could use with Click and Tag/URL, or switch to Banner for maxi lazy
	 *	
	 *	part of the Bruce Simple AS3 Banner Toolkit
	 *	
	 *	status: ready
	 *	 
	 * @version 0.6
	 * @author Bart van der Schoor @ GML
	 */
	public class Blocker 
	{
		private static var _blockers:Dictionary = new Dictionary(true);
		private static var _listening:Boolean;

		private static const TYPE:String = 'Blocker.';

		public static function full(target:Sprite, bottom:Boolean = false, color:uint = 0xFF00FF, alpha:Number = 0):MovieClip
		{
			if (!target)
			{
				Bruce.arg(TYPE + 'full() target' + Bruce.eNull);
				return null;
			}
			
			var clip:MovieClip = new MovieClip();
			if(bottom)
			{
				target.addChildAt(clip, 0);
			}
			else
			{
				target.addChild(clip);
			}
			
			_blockers[clip] = [color, alpha];
			ButtonModeC.basic(clip);
			
			if (!target.stage)
			{
				Bruce.log(TYPE + 'full() target.stage ' + Bruce.eNull);
				return clip;
			}
			
			Blocker.drawFull(clip, color, alpha, target.stage);
			
			if(_listening == false)
			{
				_listening = true;
				target.stage.addEventListener(Event.RESIZE, handleResize);
			}
			return clip;
		}

		private static function handleResize(event:Event):void
		{
			for (var key:* in _blockers)
			{
				var arr:Array = _blockers[key];
				Blocker.drawFull(Sprite(key), arr[0], arr[1], Stage(event.target));
			}
		}

		private static function drawFull(clip:Sprite, color:uint, alpha:Number = 0, stage:Stage = null):void
		{
			clip.graphics.clear();
			clip.graphics.beginFill(color, alpha);
			
			stage = stage ? stage : clip.stage;
			
			//now stage align safe!
			var rect:Rectangle = stage.getBounds(stage);
			
			var d:Number;
			d = (stage.stageWidth - rect.width) * .5;
			rect.left -= d;
			rect.right += d;
			
			d = (stage.stageHeight - rect.height) * .5;
			rect.top -= d;
			rect.bottom += d;
			
			var pa:Point = clip.globalToLocal(rect.topLeft);
			var pb:Point = clip.globalToLocal(rect.bottomRight);
			
			clip.graphics.drawRect(pa.x, pa.y, pb.x - pa.x, pb.y - pa.y);
		}

		public static function controls(clip:Object):Boolean
		{
			if (!clip)
			{
				Bruce.arg(TYPE + 'controls()' + Bruce.eclNull);
				return false;
			}
			return clip in _blockers;
		}

		public static function cancel(clip:InteractiveObject):void
		{
			if (!clip)
			{
				Bruce.arg(TYPE + 'cancel()' + Bruce.eclNull);
				return;
			}
			
			if(clip is Sprite)
			{
				Sprite(clip).graphics.clear();
			}
			
			if(clip in _blockers)
			{
				//wait for lazy onresize cancel
				delete _blockers[clip];
			}
			
			if(clip.parent)
			{
				clip.parent.removeChild(clip);
			}
			ButtonModeC.basic(clip);
		}
	}
}
